﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;   
namespace Manage_Pupils
{
    public partial class FormThemHSTrucTiep : Form
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("tblStudent");
        private DataTable dtClass = new DataTable("tblClass");
        private SqlDataAdapter da = new SqlDataAdapter();
        public FormThemHSTrucTiep()
        {
            InitializeComponent();
        }
        private void connect()
        {
            try
            {
                con = new SqlConnection("Server=IWANTWIN-PC; user id=sa; password=123456; database=QuanLyHocSinh");
                con.Open();

            }
            catch
            {
                MessageBox.Show("Ban da ket noi that bai", "Loi", MessageBoxButtons.OK);
            }
        }
        //Giai phong bo nho
        private void disconnect()
        {
            con.Close();
            con.Dispose();
            con = null;

        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select * from HocSinh";
            da.SelectCommand = command;
            da.Fill(dt);

            //Tao combox cho Ten lop

            command.CommandText = "Select * from Lop";
            da.SelectCommand = command;
            da.Fill(dtClass);
            comboBoxMaLop.DataSource = dtClass;
            comboBoxMaLop.DisplayMember = "TenLop";
            comboBoxMaLop.ValueMember = "MaLop";
            comboBoxMaLop.SelectedValue = "Malop";

        }
        private void FormThemHSTrucTiep_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }

        private void buttonLuu_Click(object sender, EventArgs e)
        {
            dt.Clear();
            try
            {
                if (!kiemtraMaNV(textMaHS.Text))  //Kiem tra MaHs co trung ko
                {
                    DataRow row = dt.NewRow();
                    if (textMaHS.Text != "")
                    {
                        row["MaHS"] = textMaHS.Text;
                        row["HoHS"] = textHoHS.Text;
                        if (textTenHS.Text != "")
                        {
                            row["TenHS"] = textTenHS.Text;
                            row["NgaySinh"] = textNgaySinh.Text;
                            if (comboGioiTinh.Text == "Nam")
                                row["GioiTinh"] = 1;
                            else
                                if (comboGioiTinh.Text == "Nu")
                                    row["GioiTinh"] = 0;
                            row["DiaChi"] = textDiaChi.Text;
                            row["DienThoai"] = textDienThoai.Text;
                            row["MaLop"] = comboBoxMaLop.SelectedValue;

                            dt.Rows.Add(row);

                            //Insert
                            SqlCommand commandInsert = new SqlCommand();
                            commandInsert.Connection = con;
                            commandInsert.CommandType = CommandType.Text;
                            commandInsert.CommandText = @"Insert HocSinh (MaHS, HoHS, TenHS, NgaySinh, GioiTinh, DiaChi, DienThoai, MaLop)
                                                        Values (@MaHS, @HoHS, @TenHS, @NgaySinh, @GioiTinh, @DiaChi, @DienThoai, @MaLop)";

                            commandInsert.Parameters.Add("@MaHS", SqlDbType.NChar, 10, "MaHS");
                            commandInsert.Parameters.Add("@HoHS", SqlDbType.NChar, 10, "HoHS");
                            commandInsert.Parameters.Add("@TenHS", SqlDbType.NChar, 10, "TenHS");
                            commandInsert.Parameters.Add("@NgaySinh", SqlDbType.Date, 50, "NgaySinh");
                            commandInsert.Parameters.Add("@GioiTinh", SqlDbType.Int, 10, "GioiTinh");
                            commandInsert.Parameters.Add("@DiaChi", SqlDbType.NChar, 10, "DiaChi");
                            commandInsert.Parameters.Add("@DienThoai", SqlDbType.NChar, 10, "DienThoai");
                            commandInsert.Parameters.Add("@MaLop", SqlDbType.NChar, 10, "MaLop");
                            da.InsertCommand = commandInsert;
                            da.Update(dt);
                            MessageBox.Show("Ban da them thanh cong !", "Thong bao", MessageBoxButtons.OK);
                            FormCapNhatHS fr = new FormCapNhatHS();
                            fr.Show();
                            Close();
                        }
                        else
                        {
                            if (MessageBox.Show("Ban phai nhap ten cua hoc sinh", "Loi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            {
                                this.Close();
                                FormCapNhatHS fr = new FormCapNhatHS();
                                fr.Show();

                            }
                            this.ActiveControl = textMaHS;
                        }

                    }
                    else
                    {
                        if (MessageBox.Show("Ban phai Nhap lai MaHS ", "Loi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            this.Close();
                            FormCapNhatHS fr = new FormCapNhatHS();
                            fr.Show();

                        }
                        this.ActiveControl = textMaHS;
                    }

                }
                else
                {
                    if (MessageBox.Show("Ma hoc sinh bi trung trong danh sach da co !", "Loi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        this.Close();
                        FormCapNhatHS fr = new FormCapNhatHS();
                        fr.Show();
                    }
                }




            }
            catch (Exception err)
            {
                MessageBox.Show("Ngay sinh sai!" + err.ToString());
            }
        }
        public Boolean kiemtraMaNV(String ma)
        {
            Boolean kt = false;
            DataTable dtkt = new DataTable();
            SqlCommand commandkt = new SqlCommand();
            commandkt.Connection = con;
            commandkt.CommandType = CommandType.Text;
            commandkt.CommandText = "Select MaHS from HocSinh where MaHS=\'" + ma + "\'";
            da.SelectCommand = commandkt;
            da.Fill(dtkt);
            if (dtkt.Rows.Count > 0) kt = true;

            return kt;
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            FormCapNhatHS fr = new FormCapNhatHS();
            fr.Show();
        }
    }
}
